<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }
    //获取分类列表
    public function getCatesList(){
        $limit = $this->request->request('limit')??10;
        $page = $this->request->request('page')??1;
        $list = Db::name("cate")->paginate($limit);
        //获取默认第一个分类的菜单
        $goodsList = $this->getGoodsList($list[0]['id']);
        $data['cateList'] = $list;
        $data['goodsList'] = $goodsList;
        $this->success('操作成功',$data);
    }
    //获取商品列表
    public function getGoodsList($cateId1=''){
        $cateId2 = $this->request->request('cate_id')??'';
        $limit = $this->request->request('limit')??10;
        $page = $this->request->request('page')??1;
        if ($cateId1){
            $cateId = $cateId1;
        }else{
            $cateId = $cateId2;
        }

        if (!$cateId){
            $this->error('操作失败',1001);
        }
        $list = Db::name("goods")->where("cate_id",$cateId)->paginate($limit);
        if ($cateId1){
          return $list;
        }
        $this->success('操作成功',$list);
    }
}
